package com.dragon.utilslibrary.dblib.clipher;

import android.content.Context;
import android.database.Cursor;

import net.sqlcipher.database.SQLiteDatabase;


/**
 * 加密数据库
 */
public class SQLMMUtil {
    private final static String TAG = "SQLMMUtil";
    private static SQLMMUtil instance;
    public static SQLMMUtil getInstance(){
        if(instance == null){
            instance = new SQLMMUtil();
        }
        return instance;
    }
    private SQLMMUtil(){

    }

    /**
     * 获取db
     * @param databasePath
     * @param mm
     * @return
     */
    public SQLiteDatabase getSqlDataBase(String databasePath, String mm){
        SQLiteDatabase encrypteddatabase = SQLiteDatabase.openOrCreateDatabase(databasePath, mm, null);
        return encrypteddatabase;
    }

    /**
     *读取加密后的数据
     */
    public void readClipherData(Context context, String databasePath, String pwd){
        try{
            SQLiteDatabase encrypteddatabase = SQLiteDatabase.openOrCreateDatabase(databasePath, pwd, null);
            String[] columns  = new String[]{
                    "*"
            };
            String sections = "**";     //这里填写查询条件
//            Cursor cursor = encrypteddatabase.query("t_userInfo", columns, sections, null, null, null, null);
            String sql = "SELECT * FROM t_userInfo";
            Cursor cursor = encrypteddatabase.rawQuery(sql, null);
            if (cursor == null){
//                Toast.makeText(MainActivity.this, "未查询到数据", Toast.LENGTH_SHORT).show();
            }else {
                if (cursor.moveToFirst()) {
                    do {
//                        byte[] name = cursor.getBlob(cursor.getColumnIndex(t.USER_INFO_USERNAME)) ;
//                        String value = cursor.getString(cursor.getColumnIndexOrThrow("value"));
//                        Log.e(TAG, "readClipherData: value = " + getRSA2048Data(context,name));
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
            encrypteddatabase.close();
        }catch (Exception e){
            e.printStackTrace();
        }


    }

}
